//
//  SampuxValidationCodeView.m
//  Sampux
//
//  Created by rzm on 16/7/5.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxValidationCodeView.h"

@interface SampuxValidationCodeView ()<UITextFieldDelegate>

@end

@implementation SampuxValidationCodeView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
    }
    return self;
}

- (void)createValidationCodeWithPlaceholder:(NSString *)placeholder andLeftImage:(NSString *)leftImg andRightImage:(NSString *)rightImg andcodeBtn:(NSString *)codeStr;
{
    
    CGFloat font = 8.;
    if (IS_IPHONE_4_OR_LESS) {
        font = 13.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 13.;
    }else if (IS_IPHONE_6)
    {
        font = 15.;
    }else
    {
        font = 16.;
    }
    UIView *backView = [[UIView alloc]initWithFrame:self.bounds];
    [self addSubview:backView];
    [UIView createLineViewInbottom:backView];
    
    _codeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _codeBtn.frame = CGRectMake(self.width - Sampux6WScale(85), Sampux6HScale(7), Sampux6WScale(85), self.height - Sampux6HScale(14));
    [_codeBtn setTitleColor:THDeFaultBlueColor forState:UIControlStateNormal];
    _codeBtn.titleLabel.font = THFont(font);
//    _codeBtn.titleLabel.textColor = THDeFaultBlueColor;
    [backView addSubview:_codeBtn];
    _codeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [_codeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    _codeBtn.hitTestEdgeInsets = UIEdgeInsetsMake(- 10 , 0, -10, -10);

    _eyeBtn = [UIButton createButton];
    [_eyeBtn setImage:imageNamed(@"eye_selected") forState:UIControlStateSelected];
    [_eyeBtn setImage:imageNamed(@"eye") forState:UIControlStateNormal];

    [backView addSubview:_eyeBtn];
    _eyeBtn.frame = CGRectMake(_codeBtn.left - 2 - 30, 0, 30, self.height);
    [_eyeBtn addTarget:self action:@selector(showPassWord:) forControlEvents:UIControlEventTouchUpInside];
    _eyeBtn.hitTestEdgeInsets = UIEdgeInsetsMake(- 10 , -10, -10, -5);

    [UIView createLineView:backView frame:CGRectMake(_codeBtn.left, backView.height/2 -Sampux6HScale(16)/2, 1, Sampux6HScale(16))];
    _codeF = [UITextField createTextFieldWithRect:CGRectMake(0, 0,_eyeBtn.left - 5, backView.height) andPlaceholder: placeholder andLeftImage:leftImg];
    [backView addSubview:_codeF];
    _codeF.delegate = self;
    _codeF.secureTextEntry = YES;


}

- (void)showPassWord:(UIButton *)btn
{
    btn.selected = !btn.selected;
//    static  NSInteger count = 0;
    
    if (btn.selected == YES) {
        self.codeF.secureTextEntry = NO;

    }else
    {
        self.codeF.secureTextEntry = YES;
    }
    
//    if (count == 0) {
//        self.codeF.secureTextEntry = NO;
//        [_eyeBtn setImage:imageNamed(@"eye_selected") forState:UIControlStateNormal];
//        count++;
//    }else
//    {
//        self.codeF.secureTextEntry = YES;
//        [_eyeBtn setImage:imageNamed(@"eye") forState:UIControlStateNormal];
//        count--;
//    }
}

@end
